package com.lam.framework.security.handler;

import java.io.IOException;
import java.io.Serializable;
import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import com.lam.common.base.domain.MsgCode;
import com.lam.common.base.domain.Result;
import com.lam.common.utils.ServletUtils;

/**
 * 认证失败处理类 返回未授权
 * 
 * @author lam
 */
@Component
public class LamAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {
	
	private static final long serialVersionUID = -8970718410437077606L;

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)throws IOException {
		
		String msg = MessageFormat.format("请求访问：{0}，认证失败，无法访问系统资源", request.getRequestURI());
		ServletUtils.outWrite(response, Result.fail(msg).code(MsgCode.UNAUTHORIZED.getCode()));
	}
}
